/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gesplan.testGCalendar;

/**
 *
 * @author Roger
 */
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;


public class testCalendar {

    private static final String ALL = "http://www.google.com/calendar/feeds/default/allcalendars/full";
//    private static final String OWN = "http://www.google.com/calendar/feeds/default/owncalendars/full";    
//    private static final String TEST = "https://www.google.com/calendar/feeds/calendari.rosersa%40gmail.com/private-be2a1248529c3eeefcfbb32bd09dcea5/basic";
//    private static final String TEST2 = "https://www.google.com/calendar/feeds/calendari.rosersa%40gmail.com/private/full";    
//    private String url_with_magic_cookie = 
//        "https://www.google.com/calendar/feeds/calendari.rosersa%40gmail.com/private-be2a1248529c3eeefcfbb32bd09dcea5/basic";    

    private static CalendarService myService;
    private static URL feedUrl;
    private static URL calendarUrl;
    
    public static void main(String[] args) throws AuthenticationException, MalformedURLException, IOException, ServiceException {
        
        myService = new CalendarService("Rosersa-Calendari");
        myService.setUserCredentials("calendari.rosersa@gmail.com", "roses2012");
        feedUrl = new URL(ALL);
        
        CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);

        System.out.println("Your calendars:\n");
        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            CalendarEntry entry = resultFeed.getEntries().get(i);
            System.out.println("\t" + entry.getTitle().getPlainText());
        }
        
        // Afegint la part del getlink agafem la url que toca per fer el POST al calendari que volem
        calendarUrl = 
                new URL(resultFeed.getEntries().get(1).getLink("alternate", "application/atom+xml").getHref());
        createEvent();
        
//        resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
//        CalendarEntry calendar = resultFeed.getEntries().get(0);
//        calendar.setTitle(new PlainTextConstruct("Prova dimarts_"));
//        calendar.setColor(new ColorProperty("#88880E"));
//        calendar.setSelected(SelectedProperty.TRUE);
//        CalendarEntry returnedCalendar = calendar.update();
//
//        System.out.println(returnedCalendar.getTitle().getPlainText());    
         
        
      /*  CalendarQuery myQuery = new CalendarQuery(feedUrl);
        myQuery.setMinimumStartTime(DateTime.parseDateTime("2012-03-16T00:00:00"));
        myQuery.setMaximumStartTime(DateTime.parseDateTime("2012-12-24T23:59:59"));
        Feed myFeed = myService.query(myQuery, Feed.class);

        CalendarEventFeed myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
        for (int i = 0; i < myFeed.getEntries().size(); i++) {
            CalendarEventEntry eventEntry = myFeed.getEntries().get(i);
            System.out.println("\t" + eventEntry.getTitle().getPlainText());
        }     */   

    }
    
    public static void updateCalendar(String name){
    
    }
    
    @SuppressWarnings("unused")
	public static void createEvent(){

        CalendarEventEntry myEntry = new CalendarEventEntry();
        
        myEntry.setTitle(new PlainTextConstruct("tasca 10 dies"));
        myEntry.setContent(new PlainTextConstruct("blablabla"));        
        DateTime startTime = DateTime.parseDateTime("2012-11-1T18:00:00");
        DateTime endTime = DateTime.parseDateTime("2012-11-10T16:00:00");
        When eventTimes = new When();
        eventTimes.setStartTime(startTime);
        eventTimes.setEndTime(endTime);
        myEntry.addTime(eventTimes);
        try {
            // Send the request and receive the response:
            CalendarEventEntry insertedEntry = myService.insert(calendarUrl, myEntry);
        } catch (IOException ex) {
            System.out.println("1: " + ex.getMessage());
        } catch (ServiceException ex) {
            System.out.println("2: " + ex.getMessage());
        }
        
    }
}
